package com.wave.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.wave.entity.*;
import com.wave.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private UserService userService;
    @Autowired
    private AuthService authService;
    @Autowired
    private InfoService infoService;
    @Autowired
    private LAFService lafService;
    @Autowired
    private DeliverService deliverService;

    @PostMapping("infoIndex")
    public JSONObject infoIndex(){
        List<Info> list = infoService.getMyInfo(null);
        Date date = DateUtil.date();    //现在时间
        String[] mouth = new String[6];
        for (int i = 0; i < 6; i++) {   //获得近6个月的月份数字，从0计数要+1
            mouth[i] = DateUtil.month(DateUtil.offsetMonth(date, i-5)) + 1 + "";
        }
        int[] num = new int[6];
//        List<Integer> num = CollUtil.newArrayList(0, 0, 0, 0, 0, 0);
        Date date6 = DateUtil.offsetMonth(date, -6);
        for (Info info : list) {
            if (info.getInfoCreateDate().after(date6)) {    // 判断是否为近6个月
                for (int n=0; n<6; n++) {
                    if (mouth[n].equals(DateUtil.month(info.getInfoCreateDate()) + 1 + "")) {
                        num[n] += 1;
                    }
                }
            }
        }
        for (int j = 0; j < 6; j++) {
            if (mouth[j].equals("0")) {
                mouth[j] = "12";    //0月修正
            }
            mouth[j] = mouth[j] + "月";
        }
        JSONObject respond = new JSONObject();
        respond.put("mouth", mouth);
        respond.put("num", num);
        return respond;
    }

    @PostMapping("lafIndex")
    public JSONObject lafIndex(){
        List<LAF> list = lafService.getMyLAF(null);
        Date date = DateUtil.date();    //现在时间
        String[] mouth = new String[6];
        for (int i = 0; i < 6; i++) {   //获得近6个月的月份数字，从0计数要+1
            mouth[i] = DateUtil.month(DateUtil.offsetMonth(date, i-5)) + 1 + "";
        }
        int[] num = new int[6];
//        List<Integer> num = CollUtil.newArrayList(0, 0, 0, 0, 0, 0);
        Date date6 = DateUtil.offsetMonth(date, -6);
        for (LAF laf : list) {
//            System.out.println(laf.getLafCreatedate());
            if (laf.getLafLastdate().after(date6)) {    // 判断是否为近6个月
                for (int n=0; n<6; n++) {
                    if (mouth[n].equals(DateUtil.month(laf.getLafLastdate()) + 1 + "")) {
                        num[n] += 1;
                    }
                }
            }
        }
        for (int j = 0; j < 6; j++) {
            if (mouth[j].equals("0")) {
                mouth[j] = "12";    //0月修正
            }
            mouth[j] = mouth[j] + "月";
        }
        JSONObject respond = new JSONObject();
        respond.put("mouth", mouth);
        respond.put("num", num);
        return respond;
    }

    @PostMapping("statsNum")
    public JSONObject statsNum(){
        int userNumV = userService.typeNum("0");  //游客
        int userNumA = userService.typeNum("1");  //管理
        int userNumT = userService.typeNum("2");  //职工
        int userNumS = userService.typeNum("3");  //学生
        int userNum = userNumV+userNumA+userNumT+userNumS;//总数
        int infoNum = infoService.countInfo(null,null,null,null);
        int infoNumUser = infoService.countInfoUser();  //资讯作者数
        int lafNum = lafService.countLaf(null,null,null);
        int lafNumSuccess = lafService.countLafState("3");
        int lafNumUser = lafService.countLafUser();     //启事作者数

        JSONObject respond = new JSONObject();
        respond.put("userNum", userNum);
        respond.put("userNumV", userNumV);
        respond.put("userNumT", userNumT);
        respond.put("userNumS", userNumS);
        respond.put("infoNum", infoNum);
        respond.put("infoNumUser", infoNumUser);
        respond.put("lafNum", lafNum);
        respond.put("lafNumSuccess", lafNumSuccess);
        respond.put("lafNumUser", lafNumUser);
        return respond;
    }

}
